package $NAMESPACE.outapi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ugeez.commons.base.base.CommonPage;
import com.ugeez.commons.base.enums.ErrorCode;
import com.ugeez.commons.base.exception.BusinessException;
import com.ugeez.user.api.facade.UserAssetFacade;
import com.ugeez.user.api.facade.UserFacade;
import com.ugeez.user.common.dto.UserAssetDto;
import com.ugeez.user.common.dto.UserDto;
import $NAMESPACE.common.dto.SomeEntityDto;
import $NAMESPACE.domain.service.SomeEntityService;
import $NAMESPACE.outapi.dto.SayHiReq;
import $NAMESPACE.outapi.dto.SayHiRes;
import $NAMESPACE.persistence.entity.SomeEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("api/out/$PROJ/test")
@Slf4j
public class TestController {
	
	@Autowired
	private SomeEntityService someEntityService;

	@Autowired
    private UserFacade userFacade;

    @Autowired
    private UserAssetFacade userAssetFacade;
	
    @PostMapping(value = "/sayHi")
    public SayHiRes sayHi(@Valid @RequestBody SayHiReq req) {
        String name = req.getName();
		if (name.equals("123")) {
			throw new BusinessException(ErrorCode.ILLEGAL_ACTION, "\"123\" is not a legal name");			
		}
        return new SayHiRes("Hello, " + name);
    }
	
    @GetMapping(value = "/data")
    public CommonPage<SomeEntityDto> getData(@RequestParam Long userId, @RequestParam Long page, @RequestParam Long pageSize) {
        IPage<SomeEntity> entityPage = someEntityService.getByUserId(userId, page, pageSize);
        return CommonPage.convert(entityPage, SomeEntityDto.class);
    }

    /**
     * 获取某个用户信息
     * @param userId
     * @return
     */
    @GetMapping(value = "/user")
    public UserDto getUser(@RequestParam Long userId) {
        UserDto res = userFacade.getById(userId);
        return res;
    }

    /**
     * 获取某个用户的某个虚拟资产
     * @param userId
     * @param currency
     * @return
     */
    @GetMapping(value = "/userAsset")
    public UserAssetDto getUserAsset(@RequestParam Long userId, @RequestParam String currency) {
        UserAssetDto res = userAssetFacade.query(userId, currency);
        return res;
    }
}



